<html>

<head>
  <title>nsIAccessibleSelectable HTML select testing</title>

  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css" />

  </style>

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <script type="application/javascript"
          src="../common.js"></script>
  <script type="application/javascript"
          src="../role.js"></script>
  <script type="application/javascript"
          src="../states.js"></script>
  <script type="application/javascript"
          src="../selectable.js"></script>

  <script type="application/javascript">

    function doTest()
    {
      //////////////////////////////////////////////////////////////////////////
      // select@size="1" aka combobox

      var id = "combobox";
      var combobox = getAccessible(id);
      var comboboxList = combobox.firstChild;
      ok(isAccessible(comboboxList, [nsIAccessibleSelectable]),
         "No selectable accessible for list of " + id);

      var select = getAccessible(comboboxList, [nsIAccessibleSelectable]);
      testSelectableSelection(select, [ "cb1_item1" ]);

      // select 2nd item
      select.addItemToSelection(1);
      testSelectableSelection(select, [ "cb1_item2" ], "addItemToSelection(1): ");

      // unselect 2nd item, 1st item gets selected automatically
      select.removeItemFromSelection(1);
      testSelectableSelection(select, [ "cb1_item1" ],
                              "removeItemFromSelection(1): ");

      // doesn't change selection
      is(select.selectAll(), false,
         "No way to select all items in combobox '" + id + "'");
      testSelectableSelection(select, [ "cb1_item1" ], "selectAll: ");

      // doesn't change selection
      select.unselectAll();
      testSelectableSelection(select, [ "cb1_item1" ], "unselectAll: ");

      //////////////////////////////////////////////////////////////////////////
      // select@size="1" with optgroups

      id = "combobox2";
      combobox = getAccessible(id);
      comboboxList = combobox.firstChild;
      ok(isAccessible(comboboxList, [nsIAccessibleSelectable]),
         "No selectable accessible for list of " + id);

      select = getAccessible(comboboxList, [nsIAccessibleSelectable]);
      testSelectableSelection(select, [ "cb2_item1" ]);

      select.addItemToSelection(1);
      testSelectableSelection(select, [ "cb2_item2" ]);

      select.removeItemFromSelection(1);
      testSelectableSelection(select, [ "cb2_item1" ]);

      is(select.selectAll(), false,
         "No way to select all items in combobox " + id + "'");
      testSelectableSelection(select, [ "cb2_item1" ]);

      select.unselectAll();
      testSelectableSelection(select, [ "cb2_item1" ]);

      //////////////////////////////////////////////////////////////////////////
      // select@size="4" aka single selectable listbox

      var id = "listbox";
      ok(isAccessible(id, [nsIAccessibleSelectable]),
         "No selectable accessible for " + id);

      select = getAccessible(id, [nsIAccessibleSelectable]);
      testSelectableSelection(select, [ ]);

      // select 2nd item
      select.addItemToSelection(1);
      testSelectableSelection(select, [ "lb1_item2" ], "addItemToSelection(1): ");

      // unselect 2nd item, 1st item gets selected automatically
      select.removeItemFromSelection(1);
      testSelectableSelection(select, [ ],
                              "removeItemFromSelection(1): ");

      // doesn't change selection
      is(select.selectAll(), false,
         "No way to select all items in single selectable listbox '" + id + "'");
      testSelectableSelection(select, [ ], "selectAll: ");

      // doesn't change selection
      select.unselectAll();
      testSelectableSelection(select, [ ], "unselectAll: ");

      //////////////////////////////////////////////////////////////////////////
      // select@size="4" with optgroups, single selectable

      id = "listbox2";
      ok(isAccessible(id, [nsIAccessibleSelectable]),
         "No selectable accessible for " + id);

      select = getAccessible(id, [nsIAccessibleSelectable]);
      testSelectableSelection(select, [ ]);

      select.addItemToSelection(1);
      testSelectableSelection(select, [ "lb2_item2" ]);

      select.removeItemFromSelection(1);
      testSelectableSelection(select, [ ]);

      is(select.selectAll(), false,
         "No way to select all items in single selectable listbox " + id + "'");
      testSelectableSelection(select, [ ]);

      select.unselectAll();
      testSelectableSelection(select, [ ]);

      //////////////////////////////////////////////////////////////////////////
      // select@size="4" multiselect aka listbox

      id = "listbox3";
      ok(isAccessible(id, [nsIAccessibleSelectable]),
         "No selectable accessible for " + id);

      select = getAccessible(id, [nsIAccessibleSelectable]);
      testSelectableSelection(select, [ ]);

      select.addItemToSelection(0);
      testSelectableSelection(select, [ "lb3_item1" ], "addItemToSelection: ");

      select.removeItemFromSelection(0);
      testSelectableSelection(select, [ ], "removeItemFromSelection: ");

      is(select.selectAll(), true,
         "All items in listbox '" + id + "' should be selected");
      testSelectableSelection(select, [ "lb3_item1", "lb3_item2"],
                              "selectAll: ");

      select.unselectAll();
      testSelectableSelection(select, [ ], "unselectAll: ");

      //////////////////////////////////////////////////////////////////////////
      // select@size="4" multiselect with optgroups

      var id = "listbox4";
      ok(isAccessible(id, [nsIAccessibleSelectable]),
         "No selectable accessible for " + id);

      select = getAccessible(id, [nsIAccessibleSelectable]);
      testSelectableSelection(select, [ ]);

      select.addItemToSelection(0);
      testSelectableSelection(select, [ "lb4_item1" ]);

      select.removeItemFromSelection(0);
      testSelectableSelection(select, [ ]);

      is(select.selectAll(), true,
         "All items in listbox '" + id + "' should be selected");
      testSelectableSelection(select, [ "lb4_item1", "lb4_item2"]);

      select.unselectAll();
      testSelectableSelection(select, [ ]);

      SimpleTest.finish();
    }

    SimpleTest.waitForExplicitFinish();
    addA11yLoadEvent(doTest);
  </script>

</head>

<body>

  <a target="_blank"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=530014"
     title="ARIA single selectable widget should implement nsIAccessibleSelectable">
    Mozilla Bug 530014
  </a><br>
  <a target="_blank"
     href="https://bugzilla.mozilla.org/show_bug.cgi?id=590176"
     title="add pseudo SelectAccessible interface">
    Mozilla Bug 590176
  </a><br>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test">
  </pre>

  <select id="combobox">
    <option id="cb1_item1">option1</option>
    <option id="cb1_item2">option2</option>
  </select>

  <select id="combobox2">
    <option id="cb2_item1">option1</option>
    <optgroup>optgroup
      <option id="cb2_item2">option2</option>
    </optgroup>
  </select>

  <select id="listbox" size="4">
    <option id="lb1_item1">option1</option>
    <option id="lb1_item2">option2</option>
  </select>

  <select id="listbox2" size="4">
    <option id="lb2_item1">option1</option>
    <optgroup>optgroup>
      <option id="lb2_item2">option2</option>
    </optgroup>
  </select>

  <select id="listbox3" size="4" multiple="true">
    <option id="lb3_item1">option1</option>
    <option id="lb3_item2">option2</option>
  </select>

  <select id="listbox4" size="4" multiple="true">
    <option id="lb4_item1">option1</option>
    <optgroup>optgroup>
      <option id="lb4_item2">option2</option>
    </optgroup>
  </select>

</body>
</html>
